package com.cryann.mqttserver.mqttProcess.mqttMsgHandler;

import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

/**
 * @author cRyann
 * @date 2018/12/28 0028 15:16
 * @description:
 */
@Slf4j
public abstract class AbstractMqttHandler {
    private AbstractMqttHandler nextHandler;

    public final boolean handleMessage(MqttMessage request) {
        if ( handlerType().contains(request.fixedHeader().messageType()) ) {
            return this.checkMsg(request);
        } else if ( this.nextHandler != null ) {
            return this.nextHandler.handleMessage(request);
        }
        return false;
    }

    public void setPrevious(AbstractMqttHandler _handler) {
        this.nextHandler = _handler;
    }

    public void setNext(AbstractMqttHandler _handler) {
        this.nextHandler = _handler;
    }

    protected abstract Set<MqttMessageType> handlerType();

    protected abstract boolean checkMsg(MqttMessage request);
}
